/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package CapaPresentacion;

import CapaLogica.Gestor;
import java.awt.ComponentOrientation;
import java.util.ArrayList;
import java.util.Date;
import java.util.TreeMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author Elayner
 */
public class PantallaArtistaEditar extends javax.swing.JFrame {
    
    
    private int id;
    private String nombre;
    private String alias;
    private String nacionalidad;
    private String ciudad;
    private String fec_nac;
    private String fec_fall;
    private String indTipo;
    private String nomEscuela;
    private int idMaestro;
    private static Gestor gestor=new Gestor();
    private Date fechaActual=new Date();

    /**
     * Creates new form PantallaArtistaAgregar
     */
    public PantallaArtistaEditar(int pidArtista) throws Exception {
        initComponents();
        loadScreenArtis(pidArtista);
        fec_nacimi.setMaxSelectableDate(fechaActual);
    }
    
    public PantallaArtistaEditar() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        ced_art = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        nom_art = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        alias_art = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        nac_art = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        fec_nacimi = new com.toedter.calendar.JDateChooser();
        jLabel6 = new javax.swing.JLabel();
        fec_fall_art = new com.toedter.calendar.JDateChooser();
        indTipoArt = new javax.swing.JComboBox();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        ciud_art = new javax.swing.JTextField();
        btn_update = new javax.swing.JButton();
        btn_cancelar = new javax.swing.JButton();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        es_art = new javax.swing.JComboBox();
        list_obras = new javax.swing.JScrollPane();
        list_obras2 = new javax.swing.JTextArea();
        list_obras1 = new javax.swing.JScrollPane();
        list_mecenas = new javax.swing.JTextArea();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Editar Artista");

        jLabel1.setText("Cedula:");

        ced_art.setEditable(false);

        jLabel2.setText("Nombre Completo:");

        jLabel3.setText("Alias:");

        jLabel4.setText("Nacionalidad:");

        jLabel5.setText("Fecha de nacimiento");

        fec_nacimi.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                fec_nacimiPropertyChange(evt);
            }
        });

        jLabel6.setText("Fecha de fallecimiento");

        indTipoArt.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "F", "R", "NR" }));

        jLabel7.setText("Tipo artista:");

        jLabel8.setText("Ciudad:");

        btn_update.setText("Actualizar");
        btn_update.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_updateActionPerformed(evt);
            }
        });

        btn_cancelar.setText("Cancelar");
        btn_cancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_cancelarActionPerformed(evt);
            }
        });

        jLabel9.setText("Lista Obras");

        jLabel10.setText("Lista Mecenas");

        jLabel11.setText("Escuela:");

        list_obras2.setColumns(20);
        list_obras2.setEditable(false);
        list_obras2.setRows(5);
        list_obras.setViewportView(list_obras2);

        list_mecenas.setColumns(20);
        list_mecenas.setEditable(false);
        list_mecenas.setRows(5);
        list_obras1.setViewportView(list_mecenas);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(26, 26, 26)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel6)
                                .addComponent(jLabel7))
                            .addGap(18, 18, 18)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(indTipoArt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(fec_fall_art, javax.swing.GroupLayout.PREFERRED_SIZE, 143, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGroup(layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(layout.createSequentialGroup()
                                    .addGap(136, 136, 136)
                                    .addComponent(btn_update))
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(jLabel11)
                                    .addGap(18, 18, 18)
                                    .addComponent(es_art, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addGap(51, 51, 51)
                            .addComponent(btn_cancelar)
                            .addGap(168, 168, 168)))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(18, 18, 18)
                        .addComponent(ced_art, javax.swing.GroupLayout.PREFERRED_SIZE, 129, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addComponent(jLabel5)
                                .addGap(18, 18, 18)
                                .addComponent(fec_nacimi, javax.swing.GroupLayout.PREFERRED_SIZE, 151, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(jLabel3)
                                    .addGap(18, 18, 18)
                                    .addComponent(alias_art, javax.swing.GroupLayout.PREFERRED_SIZE, 129, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(jLabel4)
                                    .addGap(18, 18, 18)
                                    .addComponent(nac_art, javax.swing.GroupLayout.PREFERRED_SIZE, 129, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(jLabel2)
                                    .addGap(18, 18, 18)
                                    .addComponent(nom_art, javax.swing.GroupLayout.PREFERRED_SIZE, 129, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(jLabel8)
                                    .addGap(18, 18, 18)
                                    .addComponent(ciud_art, javax.swing.GroupLayout.PREFERRED_SIZE, 129, javax.swing.GroupLayout.PREFERRED_SIZE))))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(76, 76, 76)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(list_obras1, javax.swing.GroupLayout.PREFERRED_SIZE, 241, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(list_obras, javax.swing.GroupLayout.PREFERRED_SIZE, 241, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(53, 53, 53)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel9)
                                    .addComponent(jLabel10))))))
                .addContainerGap(69, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(40, 40, 40)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel9)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(list_obras, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1)
                            .addComponent(ced_art, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(27, 27, 27)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(nom_art, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3)
                            .addComponent(alias_art, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel4)
                            .addComponent(nac_art, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(30, 30, 30)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel8)
                            .addComponent(ciud_art, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel5)
                            .addComponent(fec_nacimi, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel6)
                            .addComponent(fec_fall_art, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(indTipoArt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel7))
                        .addGap(38, 38, 38)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel11)
                            .addComponent(es_art, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(61, 61, 61)
                        .addComponent(jLabel10)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(list_obras1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 59, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btn_update)
                    .addComponent(btn_cancelar))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btn_updateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_updateActionPerformed
        try {
            if(validarPantalla()){
                id=Integer.parseInt(ced_art.getText());
                nombre=nom_art.getText();
                alias=alias_art.getText();
                nacionalidad=nac_art.getText();
                ciudad=ciud_art.getText();
                fec_nac=helperUI.getDateFormatConv(fec_nacimi.getDate());
                if(fec_fall_art.getDate()!=null){
                    fec_fall=helperUI.getDateFormatConv(fec_fall_art.getDate());
                }else{
                    fec_fall="01/01/3000";
                }
                indTipo=(String)indTipoArt.getSelectedItem();
                nomEscuela=(String)es_art.getSelectedItem();
                idMaestro=es_art.getSelectedIndex();

                gestor.actualizarArtista(id, nombre, alias, nacionalidad, ciudad, fec_nac, fec_fall, indTipo, nomEscuela);
                this.setVisible(false);
            }else{
                JOptionPane.showMessageDialog(this,(String) "Debe de llenar los campos requeridos","Error",JOptionPane.ERROR_MESSAGE);
            }
        } catch (Exception ex) {
            Logger.getLogger(PantallaArtistaEditar.class.getName()).log(Level.SEVERE, null, ex);
        }
         
         
    }//GEN-LAST:event_btn_updateActionPerformed

    private void btn_cancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_cancelarActionPerformed
        this.setVisible(false);
    }//GEN-LAST:event_btn_cancelarActionPerformed

    private void fec_nacimiPropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_fec_nacimiPropertyChange
        fec_fall_art.setMinSelectableDate(fec_nacimi.getDate());
        fec_fall_art.setMaxSelectableDate(fechaActual);
    }//GEN-LAST:event_fec_nacimiPropertyChange

    public void iniListEscuelas() throws Exception{        
        String[]listaEscuelas=helperUI.getListaEscuelas("");

        for (int i=0; i<listaEscuelas.length; i++) {
            es_art.addItem(listaEscuelas[i]);
        }
    }
    
    public void loadScreenArtis(int pidArtis) throws Exception{
        TreeMap infoArtista;
        infoArtista=gestor.buscarInfoGeneralArtista(pidArtis);
        
        if(infoArtista!=null){
            
            ced_art.setText(infoArtista.get("id")+"");
            nom_art.setText((String)infoArtista.get("nombre"));
            alias_art.setText((String)infoArtista.get("alias"));
            nac_art.setText((String)infoArtista.get("nacionalidad"));
            ciud_art.setText((String)infoArtista.get("ciudad"));
            fec_nacimi.setDate(helperUI.getDateFormatDate((String)infoArtista.get("fecha_nac")));

            if(infoArtista.get("fecha_fall")!=null)
                fec_fall_art.setDate(helperUI.getDateFormatDate((String)infoArtista.get("fecha_fall")));

            indTipoArt.setSelectedItem(infoArtista.get("pintor_fam"));
            
             iniListEscuelas();
            
            if(infoArtista.get("escuela")!=null){
                es_art.setSelectedItem(infoArtista.get("escuela"));
            }

        if((String)infoArtista.get("lista_obras")!=null)
                list_obras2.setText((String)infoArtista.get("lista_obras"));

            if((String)infoArtista.get("mecenas")!=null)
                list_mecenas.setText((String)infoArtista.get("mecenas"));
        
        }
        
        
    }
    
    public boolean validarPantalla(){
        boolean result=true;
        if(ced_art.getText().length()<=0)
            result=false;
        if(nom_art.getText().length()<=0)
            result=false;
        if(nac_art.getText().length()<=0)
            result=false;
        if(ciud_art.getText().length()<=0)
            result=false;
        if(fec_nacimi.getDate()==null)
            result=false;
        return result;
    }
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(PantallaArtistaEditar.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(PantallaArtistaEditar.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(PantallaArtistaEditar.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(PantallaArtistaEditar.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new PantallaArtistaEditar().setVisible(true);
            }
        });
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField alias_art;
    private javax.swing.JButton btn_cancelar;
    private javax.swing.JButton btn_update;
    private javax.swing.JTextField ced_art;
    private javax.swing.JTextField ciud_art;
    private javax.swing.JComboBox es_art;
    private com.toedter.calendar.JDateChooser fec_fall_art;
    private com.toedter.calendar.JDateChooser fec_nacimi;
    private javax.swing.JComboBox indTipoArt;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JTextArea list_mecenas;
    private javax.swing.JScrollPane list_obras;
    private javax.swing.JScrollPane list_obras1;
    private javax.swing.JTextArea list_obras2;
    private javax.swing.JTextField nac_art;
    private javax.swing.JTextField nom_art;
    // End of variables declaration//GEN-END:variables
}
